<template>
  <div id="tabcontrol-component">
    <div class="tittle" v-for="(item, index) in list_data" :key="index">
      <span :ref="index" @click="change_color(index, item.data)">{{
        item.name
      }}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: "TabControl",
  data() {
    return {
      list_data: [
        { name: "流行", data: "sell" },
        { name: "新款", data: "new" },
        { name: "精选", data: "pop" },
      ],
    }
  },
  mounted() {
    //一创建就添加默认值
    this.$refs[0][0].style.color = "#ff8198"
    this.$refs[0][0].style.borderBottom = "2px solid #ff8198"
    this.$emit("classify_data", this.list_data[0].data)
  },
  methods: {
    change_color(val, data) {
      // 排他思想
      this.list_data.forEach((item, index) => {
        this.$refs[index][0].style.color = ""
        this.$refs[index][0].style.borderBottom = "none"
      })

      this.$refs[val][0].style.color = "#ff8198"
      this.$refs[val][0].style.borderBottom = "2px solid #ff8198"

      //子传父
      this.$emit("classify_data", data)
    },
  },
}
</script>

<style lang="less" scoped>
#tabcontrol-component {
  display: flex;
  height: 35px;
  text-align: center;
  line-height: 35px;
  .tittle {
    flex: 1;
    span {
      color: #666666;
      padding-bottom: 5px;
    }
  }
}
</style>
